/**
 * <p><b>Title:</b> SysDictTypeController.java </p>
 * @Package com.wondertek.oes.bc.setting.controller
 */
package com.wondertek.poms.setting.controller;


import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.common.controller.BaseController;
import com.wondertek.poms.common.exception.BusinessException;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.common.log.MethodLog;
import com.wondertek.poms.common.utils.validate.ValidateFilter;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.ISysDictDataService;
import com.wondertek.poms.core.service.ISysDictTypeService;
import com.wondertek.poms.dao.po.SysDictData;
import com.wondertek.poms.dao.po.SysDictType;
import com.wondertek.poms.setting.vo.SysDictTypeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * <p><b>SysDictTypeController Description:</b> 字典类型表Controller层实现</p>
 * @author wzq
 * <p><b>DATE</b> 2019-08-13 11:02:01</p>
 */
@Api("字典类型")
@RestController
@Scope("prototype")
@RequestMapping(value="/dict-type")
public class SysDictTypeController extends BaseController {

	@Autowired
	private ISysDictTypeService sysDictTypeService;
	@Autowired
	private ISysDictDataService sysDictDataService;

	@ApiOperation("字典类型列表")
	@GetMapping("/list")
	@ResponseBody
	@MethodLog(module = "字典类型列表")
	public ResultBean list(SysDictTypeVo sysDictTypeVo,
						   @RequestParam(defaultValue = "1") int page,
						   @RequestParam(defaultValue = "20") int rows,
						   @RequestParam(defaultValue = "updatedTime") String sidx,
						   @RequestParam(defaultValue = "desc") String sord) {
		SysDictType sysDictType = new SysDictType();
		BeanUtils.copyProperties(sysDictTypeVo, sysDictType);

		Page<SysDictType> pages = sysDictTypeService
				.findAll(sysDictType, page, rows, sord, sidx);

		return PageBean.ok(pages.getTotalPages(), pages.getTotalElements(), pages.getContent());
	}

	@ApiOperation("字典类型新增")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "name", required = true),
			@ApiImplicitParam(name = "type", required = true),
			@ApiImplicitParam(name = "status", required = true),
			@ApiImplicitParam(name = "createdBy", required = true),
			@ApiImplicitParam(name = "createdTime", required = true),
			@ApiImplicitParam(name = "updatedBy", required = true),
			@ApiImplicitParam(name = "updatedTime", required = true),
			@ApiImplicitParam(name = "remarks", required = true),
	})
	@PostMapping(value = "/add")
	@ResponseBody
	@MethodLog(module = "字典类型新增", insertDB = true)
	public ResultBean add(@RequestBody SysDictTypeVo sysDictTypeVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(sysDictTypeVo, Constants.VALIDATE_DEFAULT);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}

		if (isRepeat(sysDictTypeVo))
			return ResultBean.error("字典类型" + MessageCodes.get(MessageCodes.M980014));

		SysDictType sysDictType = new SysDictType();
		BeanUtils.copyProperties(sysDictTypeVo, sysDictType);

		SysDictType.putTimeAndUser(sysDictType);
		sysDictTypeService.save(sysDictType);
		return ResultBean.ok(sysDictType);
	}

	/**
	 * <p><b>Title:</b> isRepeat</p>
	 * <p><b>Description:</b> 判断字典类型表名称是否重复</p>
	 *
	 * @param sysDictTypeVo
	 * @author douzi
	 */
	private boolean isRepeat(SysDictTypeVo sysDictTypeVo) {
		SysDictType temp = new SysDictType();
		temp.setType(sysDictTypeVo.getType());
		Optional<SysDictType> sysDictTypeOpt = sysDictTypeService.findOne(Example.of(temp));
		if (sysDictTypeOpt.isPresent()) {
			SysDictType sysDictType = sysDictTypeOpt.get();
			if (sysDictTypeVo.getId() != null && !sysDictTypeVo.getId().equals(sysDictType.getId()))
				return true;
			else return sysDictTypeVo.getId() == null;
		}
		return false;
	}

	@ApiOperation("字典类型详情")
	@ApiImplicitParam(name = "id", required = true, paramType = "query", dataType = "Long")
	@GetMapping(value = "/get")
	@ResponseBody
	public ResultBean get(Long id) {
		SysDictType sysDictType = sysDictTypeService.get(id);
		return ResultBean.ok(sysDictType);
	}

	@ApiOperation("字典类型变更")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", required = true),
			@ApiImplicitParam(name = "name", required = true),
			@ApiImplicitParam(name = "type", required = true),
			@ApiImplicitParam(name = "status", required = true),
			@ApiImplicitParam(name = "createdBy", required = true),
			@ApiImplicitParam(name = "createdTime", required = true),
			@ApiImplicitParam(name = "updatedBy", required = true),
			@ApiImplicitParam(name = "updatedTime", required = true),
			@ApiImplicitParam(name = "remarks", required = true),
	})
	@PutMapping(value = "/edit")
	@ResponseBody
	@MethodLog(module = "字典类型变更", insertDB = true, opterate = 2)
	//TODO (需要删除不需要的属性列)
	public ResultBean update(@RequestBody SysDictTypeVo sysDictTypeVo) {
		List<String> messageList = ValidateFilter.getFilterMessage(sysDictTypeVo, Constants.VALIDATE_UPDATE);
		if (!messageList.isEmpty()) {
			return ResultBean.error(messageList.toString());
		}
		//字典不能修改type，前台会给我传个null，导致我下面验证isRepeat永远返回false，所以我在操作之前先赋值type
		SysDictType bcDict1 = sysDictTypeService.get(sysDictTypeVo.getId());
		sysDictTypeVo.setType(bcDict1.getType());
		if (isRepeat(sysDictTypeVo))
			return ResultBean.error("字典类型" + MessageCodes.get(MessageCodes.M980014));

		SysDictType sysDictType = new SysDictType();
		BeanUtils.copyProperties(sysDictTypeVo, sysDictType);
		return ResultBean.ok(sysDictTypeService.merge(sysDictType));
	}



	@ApiOperation("字典类型启用和停用")
	@ApiImplicitParam(name = "id", required = true, paramType = "query", dataType = "Long")
	@GetMapping(value = "/update-status")
	@ResponseBody
	@MethodLog(module = "字典类型启用和停用", insertDB = true, opterate = 2)
	public ResultBean updateStatus(Long id, Integer status) {
		if (status == null || id ==null) {
			return ResultBean.error("类型id或状态传值不能为空！");
		}
		if (!(status == 0 || status == 2)) {
			return ResultBean.error("状态值传值错误！");
		}
		SysDictType dictType = sysDictTypeService.findById(id);
		dictType.setDeleted(status);
		sysDictTypeService.deleteAndStopLogic(dictType);
		return ResultBean.ok();
	}


	@ApiOperation("字典类型删除")
	@ApiImplicitParam(name = "id", required = true, paramType = "query", dataType = "Long")
	@DeleteMapping(value = "/del")
	@ResponseBody
	@MethodLog(module = "字典类型删除", insertDB = true, opterate = 3)
	public ResultBean delete(Long id) {
		if (id ==null) {
			return ResultBean.error("类型id传值不能为空！");
		}
		SysDictType dictType = sysDictTypeService.findById(id);
		if(dictType.getDeleted()==0){
			return ResultBean.error("该类型为启用状态，不能删除！");
		}
		dictType.setDeleted(1);
		SysDictType.putTimeAndUser(dictType);
		sysDictTypeService.save(dictType);
		List<SysDictData> sysDictDataList = sysDictDataService.findAllByType(dictType.getType());
		for (SysDictData sysDictData : sysDictDataList) {
			sysDictDataService.deleteById(sysDictData);
		}
		return ResultBean.ok();
	}
}
